package com.ruoyi.rural.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.rural.domain.RuralApplicationBenefitAnagement;
import com.ruoyi.rural.domain.RuralBenefitAnagement;
import com.ruoyi.rural.service.IRuralApplicationBenefitAnagementService;
import com.ruoyi.rural.service.IRuralBenefitAnagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 救济金申领Controller
 * 
 * @author ruoyi
 * @date 2022-02-06
 */
@RestController
@RequestMapping("/applicationBenefitAnagement")
public class RuralApplicationBenefitAnagementController extends BaseController
{
    @Autowired
    private IRuralApplicationBenefitAnagementService ruralApplicationBenefitAnagementService;

    @Autowired
    private IRuralBenefitAnagementService ruralBenefitAnagementService;

    /**
     * 查询救济金申领列表
     */
    @RequiresPermissions("rural:applicationBenefitAnagement:list")
    @GetMapping("/list")
    public TableDataInfo list(RuralApplicationBenefitAnagement ruralApplicationBenefitAnagement)
    {
        startPage();
        List<RuralApplicationBenefitAnagement> list = ruralApplicationBenefitAnagementService.selectRuralApplicationBenefitAnagementList(ruralApplicationBenefitAnagement);
        return getDataTable(list);
    }

    /**
     * 导出救济金申领列表
     */
    @RequiresPermissions("rural:applicationBenefitAnagement:export")
    @Log(title = "救济金申领", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RuralApplicationBenefitAnagement ruralApplicationBenefitAnagement)
    {
        List<RuralApplicationBenefitAnagement> list = ruralApplicationBenefitAnagementService.selectRuralApplicationBenefitAnagementList(ruralApplicationBenefitAnagement);
        ExcelUtil<RuralApplicationBenefitAnagement> util = new ExcelUtil<RuralApplicationBenefitAnagement>(RuralApplicationBenefitAnagement.class);
        util.exportExcel(response, list, "救济金申领数据");
    }

    /**
     * 获取救济金申领详细信息
     */
    @RequiresPermissions("rural:applicationBenefitAnagement:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(ruralApplicationBenefitAnagementService.selectRuralApplicationBenefitAnagementById(id));
    }

    /**
     * 新增救济金申领
     */
    @RequiresPermissions("rural:applicationBenefitAnagement:add")
    @Log(title = "救济金申领", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RuralApplicationBenefitAnagement ruralApplicationBenefitAnagement)
    {
        ruralApplicationBenefitAnagement.setClaimStatus("1");
        //  修改救济金类型总量
        RuralBenefitAnagement ruralBenefitAnagement = new RuralBenefitAnagement();
        ruralBenefitAnagement.setBenefitType(ruralApplicationBenefitAnagement.getBenefitType());
        List<RuralBenefitAnagement> ruralBenefitAnagements = ruralBenefitAnagementService.selectRuralBenefitAnagementList(ruralBenefitAnagement);
        // 若不为空则修改总金额,否则
        RuralBenefitAnagement ruralBenefitAnagement1;
        if (!ruralBenefitAnagements.isEmpty()){
            ruralBenefitAnagement1 = ruralBenefitAnagements.get(0);
            BigDecimal totalAmount = new BigDecimal(ruralBenefitAnagement1.getBenefitAmount());
            BigDecimal amount = new BigDecimal(ruralApplicationBenefitAnagement.getBenefitAmount());
            if (totalAmount.compareTo(amount) < 1){
                return AjaxResult.error("申领金额超过已有金额！");
            }
            ruralApplicationBenefitAnagementService.insertRuralApplicationBenefitAnagement(ruralApplicationBenefitAnagement);
        }else {
            return AjaxResult.error("该救济金类型没有配置银行信息！");
        }
        return AjaxResult.success("新增成功！");
    }

    /**
     * 修改救济金申领
     */
    @RequiresPermissions("rural:applicationBenefitAnagement:edit")
    @Log(title = "救济金申领", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RuralApplicationBenefitAnagement ruralApplicationBenefitAnagement)
    {
        return toAjax(ruralApplicationBenefitAnagementService.updateRuralApplicationBenefitAnagement(ruralApplicationBenefitAnagement));
    }

    /**
     * 确认救济金已发
     */
    @Transactional
    @RequiresPermissions("rural:applicationBenefitAnagement:edit")
    @Log(title = "救济金申领", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/confirm")
    public AjaxResult confirm(@RequestBody RuralApplicationBenefitAnagement ruralApplicationBenefitAnagement)
    {
        //  修改捐赠状态并更改
        ruralApplicationBenefitAnagement.setClaimStatus("2");
        ruralApplicationBenefitAnagementService.updateRuralApplicationBenefitAnagement(ruralApplicationBenefitAnagement);
        //  修改救济金类型总量
        RuralBenefitAnagement ruralBenefitAnagement = new RuralBenefitAnagement();
        ruralBenefitAnagement.setBenefitType(ruralApplicationBenefitAnagement.getBenefitType());
        List<RuralBenefitAnagement> ruralBenefitAnagements = ruralBenefitAnagementService.selectRuralBenefitAnagementList(ruralBenefitAnagement);
        // 若不为空则修改总金额,否则
        RuralBenefitAnagement ruralBenefitAnagement1;
        if (!ruralBenefitAnagements.isEmpty()){
            ruralBenefitAnagement1 = ruralBenefitAnagements.get(0);
            BigDecimal totalAmount = new BigDecimal(ruralBenefitAnagement1.getBenefitAmount());
            BigDecimal amount = new BigDecimal(ruralApplicationBenefitAnagement.getBenefitAmount());
            if (totalAmount.compareTo(amount) < 1){
                return AjaxResult.error("申领金额超过已有金额！");
            }
            ruralBenefitAnagement1.setBenefitAmount(totalAmount.subtract(amount).toString());
            ruralBenefitAnagementService.updateRuralBenefitAnagement(ruralBenefitAnagement1);
        }else {
            return AjaxResult.error("该救济金类型没有配置银行信息！");
        }
        return AjaxResult.success("确认成功！");
    }

    /**
     * 删除救济金申领
     */
    @RequiresPermissions("rural:applicationBenefitAnagement:remove")
    @Log(title = "救济金申领", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(ruralApplicationBenefitAnagementService.deleteRuralApplicationBenefitAnagementByIds(ids));
    }
}
